package xnj.lazydog.btcontroller.ControlViews;


import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class CSVFile {

    private static final String TAG = "CSVFile";

    public File file;

    public int rows,cols;

    List<List<String>> values;

    public CSVFile(String path)
    {
        file = new File(path);

        if(!file.exists()) {
            try {
                //Log.w(TAG, "CSVFile: file not found" );
                boolean b = file.createNewFile();
                if(!b)
                {
                    cols = -1;
                    rows = -1;
                    return;
                }
            } catch (IOException e) {
                e.printStackTrace();
                cols = -1;
                rows = -1;
                return;
            }
        }

        boolean b =load(file);

        if(!b)
        {
            rows = -1;
            cols = -1;
        }else
        {
            rows = values.size();
            if(rows==0)
                cols = 0;
            else {
                for(List<String> list:values) {
                    cols =list.size();
                    //Log.w(TAG, "CSVFile:  cols "+cols );
                }
            }
        }
    }
    public CSVFile(File file)
    {

        this.file = file;
        if(!file.exists()) {
            try {
                //Log.w(TAG, "CSVFile: file not found" );
                boolean b = file.createNewFile();
                if(!b)
                {
                    cols = -1;
                    rows = -1;
                    return;
                }
            } catch (IOException e) {
                e.printStackTrace();
                cols = -1;
                rows = -1;
                return;
            }
        }

        boolean b =load(file);

        if(!b)
        {
            rows = -1;
            cols = -1;
        }else
        {
            rows = values.size();
            if(rows==0)
                cols = 0;
            else {
                for(List<String> list:values) {
                    cols =list.size();
                    //Log.w(TAG, "CSVFile:  cols "+cols );
                }
            }
        }
    }
    public boolean load(File file)
    {
        Log.w(TAG, "load: "+file.getPath() +"  "+file.length());
        String buff = null;
        try {

            InputStream inputStream = new FileInputStream(file);
            //inputStream.reset();

            byte [] buff_byte = new byte[(int)file.length()];

            int length = inputStream.read(buff_byte);

            inputStream.close();
            Log.w(TAG, "load: "+length );

            if(length<=0)
                return false;

            buff = new String(buff_byte);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            cols = -1;
            rows = -1;
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            cols = -1;
            rows = -1;
            return false;
        }

        values = new ArrayList<>();

        String[] rowBuffer = buff.split("\n");

        for(String row:rowBuffer)
        {

            List<String> rowList = getRow(row);
            if(rowList.size()>0)
                values.add(rowList);

        }
        return true;
    }

    private List<String> getRow(String row)
    {
        String[] sa = row.split(",");
        //List<String> list = new ArrayList<>();
        return new ArrayList<>(Arrays.asList(sa));
    }

    public void resize(int newRows,int newCols)
    {
        if(newRows!=rows)
        {
            if(newRows>rows)
            {
                for(int i=0;i<(newRows-rows);i++)
                {
                    values.add(new ArrayList<String>());
                }
            }else
            {
                for(int i=0;i<(rows-newRows);i++)
                {
                    values.remove(values.size()-1);
                }
            }
        }

        for(List<String> l:values)
        {
            int c = l.size();

            if(newCols>c)
            {
                for(int i=0;i<newCols-c;i++)
                    l.add("");
            }else if(newCols<c)
            {
                for(int i=0;i<c-newCols;i++)
                    l.remove(l.size()-1);
            }
        }

        cols = newCols;
        rows = newRows;
    }

    public void setValue(int row, int col, String value)
    {
        int maxCol = cols,maxRow = rows;
        if((row+1)>maxRow)
            maxRow = row+1;
        if((col+1)>maxCol)
            maxCol = col+1;

        if(maxCol!=cols||maxRow!=rows)
            resize(maxRow,maxCol);

        values.get(row).set(col,value);
    }

    public void  setValueList(List<List<String>> list)
    {
        this.values = list;
        rows = list.size();
        if(rows>0)
            cols = list.get(0).size();

    }

    public String getValue(int row, int col)
    {
        if(row>=rows||col>=cols)
            return "null";
        return values.get(row).get(col);
    }

    public boolean saveLittle()
    {
        StringBuilder buff = new StringBuilder();

        rows = values.size();
        if(rows>0)
            cols = values.get(0).size();


        if(cols>0)
        {
            for(int i=0;i<rows;i++) {
                if(i>0)
                    buff.append("\n");
                for (int j = 0; j < cols; j++) {
                    //Log.w(TAG, "show: " + values.get(i).get(j));
                    buff.append(values.get(i).get(j));
                    if(j<cols-1)
                        buff.append(",");
                }
            }
        }else
            buff.append("");

        Log.w(TAG, "saveLittle: "+buff.toString() );

        try {
            FileWriter fileWriter =new FileWriter(file);
            fileWriter.write("");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        byte[] bytes = buff.toString().getBytes();
        try {
            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(bytes);
            outputStream.flush();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    public void show()
    {
        StringBuilder buff = new StringBuilder();
        if(cols>0)
        {
            for(int i=0;i<rows;i++) {

                buff.append("\n");

                for (int j = 0; j < cols; j++) {
                    //Log.w(TAG, "show: " + values.get(i).get(j));
                    buff.append(values.get(i).get(j));
                    buff.append("  ");
                }
            }
        }
        Log.w(TAG, "show: "+buff.toString() );
    }
    //public CSVFile()

}
